package com.digitalchina.lxnsy.activity.old;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.activity.management.EnterpriseAuthActivity;
import com.digitalchina.lxnsy.activity.management.ManagementDetailsV2Activity;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.URL;
import com.digitalchina.lxnsy.toolkit.ActivityTaskManager;
import com.digitalchina.lxnsy.toolkit.EncryptUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.OkHttpUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.digitalchina.lxnsy.widget.EditTextWithClear;
import com.digitalchina.lxnsy.xml.AdminXML;
import com.digitalchina.lxnsy.xml.UserXML;
import com.lzy.okhttputils.model.HttpParams;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2019/4/27 13:20
 * 修改备注：
 * ================================================
 */
public class LoginAdminActivity extends BaseActivity {

    @Bind(R.id.login_et_user_name)
    EditTextWithClear loginEtUserName;
    @Bind(R.id.login_et_user_pwd)
    EditTextWithClear loginEtUserPwd;
    @Bind(R.id.login_btn_login)
    Button loginBtnLogin;

    @Override
    public void setLayout() {
        setTitle("企业管理登录");
        setContentLayout(R.layout.activity_login_admin);
    }

    @Override
    public void initMember() {

    }

    @Override
    public void addListener() {
        loginBtnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                login();
            }
        });
    }

    private void login() {

        String loginName = StringUtil.getEditText(loginEtUserName);
        String loginPwd = StringUtil.getEditText(loginEtUserPwd);

        if (StringUtil.isStrEmpty(loginName)) {
            showToast("请输入用户名");
            return;
        }

        if (StringUtil.isStrEmpty(loginPwd)) {
            showToast("请输入密码");
            return;
        }

        HttpParams params = new HttpParams();
        params.put("loginName", loginName);
        params.put("p_sha", EncryptUtil.SHA1(loginPwd));
        params.put("loginChannel", "1");
        params.put("mobileType", "1");

        OkHttpUtil.post(activity, URL.URL_ENTERPRISE_USER_LOGIN, "正在登录", params, mHandler,
                MSG_S_0, MSG_F_0);
    }

    @Override
    public void setHandler() {

        mHandler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message message) {
                switch (message.what) {
                    case MSG_S_0:
                        showToast("登录成功");
                        String isAudit = "";
                        try {
                            String body = new JSONObject(message.obj.toString()).getString("body");
                            // 设置用户类型 1表示admin
                            AdminXML.setUserType(activity,"1");
                            // 是否审核通过 0 审核未通过  1 审核通过  2 待审核  3 已注册 未提交
                            isAudit = new JSONObject(body).getString("isAudit");
                            AdminXML.setIsAudit(activity, isAudit);

                            AdminXML.setEnterpriseId(activity, new JSONObject(body).getString("enterpriseId"));
                            AdminXML.setUserAccId(activity, new JSONObject(body).getString("userAccId"));

                            UserXML.setUserId(activity, new JSONObject(body).getString("userAccId"));
                            UserXML.setLOGIN(activity, new JSONObject(body).getString("LOGIN"));
                            UserXML.setSECURELOGIN(activity, new JSONObject(body).getString("SECURELOGIN"));
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                        if("1".equals(isAudit)){
                            //审核通过
                            GotoUtil.gotoActivity(activity, ManagementDetailsV2Activity.class);
                            ActivityTaskManager.getInstance().closeAllActivityExceptOne(ManagementDetailsV2Activity.class);
                        }else{
                            //其他
                            GotoUtil.gotoActivity(activity, EnterpriseAuthActivity.class);
                            ActivityTaskManager.getInstance().closeAllActivityExceptOne(EnterpriseAuthActivity.class);
                        }

                        break;
                    case MSG_F_0:
                        showToast(message.obj.toString());
                        break;
                }
                return false;
            }
        });
    }

    @Override
    public void callBack(Object object) {

    }

    @Override
    public void rightOnClick() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
